package io.wmy.supex.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashSet;
import java.util.UUID;


public class StringUtil extends StringUtils {

    private static final Logger logger = LoggerFactory.getLogger(StringUtil.class);


    public static boolean isEnable(String value) {
        if(value == null) {
            return false;
        }
        return value.equals("true") || value.equals("1") || value.equals("y") || value.equals("yes");
    }

    public static boolean isDisable(String value) {
        return !isEnable(value);
    }


    /**
     * 等于一组字符串中的任何一个则返回true,否则返回false
     *
     * @param target
     * @param strs
     * @return
     */
    public static boolean equals(String target, String... strs) {
        for (String str : strs) {
            if (StringUtils.equals(target, str)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 连接指定的字符串
     *
     * @param strs
     * @return
     */
    public static String append(String... strs) {
        StringBuilder builder = new StringBuilder();
        for (String s : strs) {
            builder.append(s);
        }
        return builder.toString().trim();
    }

    /**
     * 将一组字符串用指定的字符分隔得到一个字符串 如mgr_test_value
     *
     * @param split
     * @param values
     * @return
     */
    public static String join(String split, String... values) {
        StringBuilder builder = new StringBuilder();
        for (String s : values) {
            builder.append(s).append(split);
        }
        return removeEndCharacter(builder.toString().trim(), split);
    }

    /**
     * 去掉字符串结尾的字符
     *
     * @param string
     * @param endCharacter
     * @return
     */
    public static String removeEndCharacter(String string, String endCharacter) {
        if (StringUtils.isNotBlank(string) && string.endsWith(endCharacter)) {
            string = string.substring(0, string.length() - endCharacter.length());
        }
        return string;
    }

    /**
     * 所有参数都不为空才返回false,否则返回true
     *
     * @param strs
     * @return
     */
    public static boolean hasEmpty(String... strs) {
        for (String str : strs) {
            if (StringUtils.isEmpty(str)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 所有参数都不为空才返回false,否则返回true
     *
     * @param strs
     * @return
     */
    public static boolean hasBlank(String... strs) {
        for (String str : strs) {
            if (StringUtils.isBlank(str)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 任何参数不为空，返回true，否则返回false
     *
     * @param strs
     * @return
     */
    public static boolean isNotBlank(String... strs) {
        if (null == strs) {
            return false;
        }
        for (String str : strs) {
            if (StringUtils.isNotBlank(str)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 只要有一个参数不为空就返回false,所有参数都为空返回true
     *
     * @param strs
     * @return
     */
    public static boolean allEmpty(String... strs) {
        for (String str : strs) {
            if (StringUtils.isNotEmpty(str)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 只要有一个参数不为空就返回false,所有参数都为空返回true
     *
     * @param strs
     * @return
     */
    public static boolean allBlank(String... strs) {
        if (null == strs) {
            return true;
        }
        for (String str : strs) {
            if (StringUtils.isBlank(str)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 首字母转大写
     *
     * @param oldStr
     * @return
     */
    public static String firstUpper(String oldStr) {
        byte[] items = oldStr.getBytes();
        items[0] = (byte) ((char) items[0] - ('a' - 'A'));
        return new String(items);
    }

    /**
     * 将带下划线的值转成骆驼命名法 如 user_name =>userName
     */
    public static String toCamelCase(String oldStr) {
        if (!oldStr.contains("_")) {
            return oldStr;
        }
        String[] keys = oldStr.split("\\_");
        StringBuilder sb = new StringBuilder(keys[0]);
        for (int i = 1; i < keys.length; i++) {
            sb.append(StringUtil.firstUpper(keys[i]));
        }
        return sb.toString();
    }

    public static String getUUID() {
        String uuid = UUID.randomUUID().toString();
        return uuid.replaceAll("-", "");
    }

    /**
     * 字符串长度判断
     *
     * @param str
     * @param min
     * @param max
     * @return
     */
    public static boolean lengthBetween(String str, int min, int max) {
        if (StringUtil.isBlank(str)) {
            return true;
        }
        int length = str.trim().length();
        if (length >= min && length <= max) {
            return true;
        }
        return false;
    }

    /**
     * 字符串转换为integer，如果不符合数字格式，返回null
     *
     * @param str
     * @return
     */
    @SuppressWarnings("finally")
    public static Integer strToInteger(String str) {
        if (isBlank(str)) {
            return null;
        }
        Integer result = null;
        try {
            result = Integer.valueOf(str);
        } catch (NumberFormatException e) {
            logger.error("字符串转换数字出错，字符串：{}；错误信息：{}", str, e);
        } catch (Exception e) {
            logger.error("字符串转换数字出错，字符串：{}；错误信息：{}", str, e);
        } finally {
            return result;
        }
    }

    /**
     * 将停车时长转换为中文显示，2时12分12秒
     *
     * @param parkTime ,格式为小时数，分钟数，秒数，例如：3422323（342小时23分23秒）
     * @return
     */
    public static String parkTimeToChineseTime(String parkTime) {
        if (isBlank(parkTime)) {
            return "00时00分00秒";
        }
        int length = parkTime.length();
        StringBuffer timeSB = new StringBuffer();
        if (length >= 6) {
            timeSB.append(parkTime.substring(0, length - 4)).append("时");
            timeSB.append(parkTime.substring(length - 4, length - 2)).append("分");
            timeSB.append(parkTime.substring(length - 2)).append("秒");
        }
        return timeSB.toString();
    }

    /**
     * 手机号混淆,隐藏中间四位
     *
     * @param phone
     * @return
     */
    public static String phoneConfusion(String phone) {
        if (StringUtil.isBlank(phone)) {
            return "****";
        }
        if (phone.length() < 3) {
            return phone;
        }
        if (phone.length() < 7) {
            return phone.substring(0, 3) + "****";
        }
        return phone.substring(0, 3) + "****" + phone.substring(7);
    }

    /**
     * 根据车牌号获取模糊车牌号数组
     *
     * @param carNum
     * @return
     */
    public static String[] getSimilarCarNumArray(String carNum) {
        if (isBlank(carNum) || carNum.length() != 7) {
            return null;
        }

        String[] carNumArray = new String[7];
        carNumArray[0] = "_" + carNum.substring(1, 7);
        carNumArray[1] = String.valueOf(carNum.substring(0, 1)) + "_" + carNum.substring(2, 7);
        carNumArray[2] = String.valueOf(carNum.substring(0, 2)) + "_" + carNum.substring(3, 7);
        carNumArray[3] = String.valueOf(carNum.substring(0, 3)) + "_" + carNum.substring(4, 7);
        carNumArray[4] = String.valueOf(carNum.substring(0, 4)) + "_" + carNum.substring(5, 7);
        carNumArray[5] = String.valueOf(carNum.substring(0, 5)) + "_" + carNum.substring(6, 7);
        carNumArray[6] = String.valueOf(carNum.substring(0, 6)) + "_";
        return carNumArray;
    }

    /**
     * 验证字符串数组是否有重复字符串
     *
     * @param strs
     * @return
     */
    public static boolean isRepeat(String... strs) {
        if (null == strs || strs.length < 2) {
            return false;
        }
        return new HashSet<>(Arrays.asList(strs)).size() != strs.length;
    }

    /**
     * 验证字符串数组中字符串长度是否超过指定长度
     *
     * @param maxLength 最大字符串长度
     * @param strs
     * @return
     */
    public static boolean isExceedMaxlength(int maxLength, String... strs) {
        if (null == strs) {
            return false;
        }
        for (String str : strs) {
            if (null != str && str.length() > maxLength) {
                return true;
            }
        }
        return false;
    }

    /**
     * 截断字符串
     *
     * @param maxLength 最大字符串长度
     * @param str
     * @return
     */
    public static String trunc(int maxLength, String str) {
        return isNotBlank(str) && str.length() > maxLength ? str.substring(0, maxLength) : str;
    }

}
